package com.blog.dao.impl;

import com.blog.dao.BaseDao;
import com.blog.dao.TagDao;
import com.blog.pojo.Article;
import com.blog.pojo.Tag;
import com.blog.utils.SqlIUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TagDaoImpl extends BaseDao implements TagDao {

    @Override
    public List<Tag> getTags() {
        List<Tag> tagList=new ArrayList<Tag>();
        try {
            String sql = "select DISTINCT tag from tb_tag";
            ResultSet resultSet = this.execQuery(sql, null);
            while (resultSet.next()) {
                String tagName = resultSet.getString(1);
                Tag tag = new Tag();
                tag.setTag(tagName);
                tagList.add(tag);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConn(conn,resultSet,ps);
        }
        return tagList;
    }

    @Override
    public List<Tag> getAllTags() {
        List<Tag> tagList=new ArrayList<Tag>();
        try {
            String sql = "select * tag from tb_tag";
            ResultSet resultSet = this.execQuery(sql, null);
            while (resultSet.next()) {
                Integer id = resultSet.getInt(1);
                String tagName = resultSet.getString(2);
                Tag tag = new Tag(id,tagName);
                tagList.add(tag);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConn(conn,resultSet,ps);
        }
        return tagList;
    }

    @Override
    public int add(Tag tag) {
        if(SqlIUtils.isSqlValid(tag.getTag()+" "+tag.toString())){
            return 0;
        }
        String sql="insert into tb_tag values(?,?)";
        Object[] parms={tag.getId(),tag.getTag()};
        return this.execUpdate(sql,parms);
    }

    @Override
    public int deleteTag(String articleTag) {
        if(SqlIUtils.isSqlValid(articleTag)){
            return 0;
        }
        String sql = "delete from tb_tag  where tag= ?";
        Object[] parms = {articleTag};
        return this.execUpdate(sql, parms);
    }

    @Override
    public List<Tag> getTagsById(String id) {
        List<Tag> tagList=new ArrayList<Tag>();
        if(SqlIUtils.isSqlValid(id)){
            return tagList;
        }
        try {
            String sql = "select tag from tb_tag inner join tb_article on tb_article.id=tb_tag.id where tb_article.id=?";
            Object[] parms={id};

            ResultSet resultSet = this.execQuery(sql, parms);
            while (resultSet.next()) {
                String tagName = resultSet.getString(1);
                Tag tag = new Tag();
                tag.setTag(tagName);
                tagList.add(tag);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConn(conn,resultSet,ps);
        }
        return tagList;
    }

    @Override
    public int editTag(String tagName, String newTag) {
        if(SqlIUtils.isSqlValid(tagName+" "+newTag)){
            return 0;
        }
        String sql="update tb_tag set tag=? where tag=?";
        Object[]parms={newTag,tagName};
        return this.execUpdate(sql,parms);
    }
}
